<?php 

class Application_Form_Inscription extends Zend_Form
{
	public function __construct($options = null)
	{
		parent::__construct($options);

		$this->setName('inscription');

		$login = new Zend_Form_Element_Text("login", array('size' => 25));
		$loginDoesntExist = new Zend_Validate_Db_NoRecordExists('membres', 'login');
		$login ->setLabel('Login')
		  ->setRequired(true)
		  ->addFilter('StripTags')
		  ->addFilter('StringTrim')
		  ->addValidator('NotEmpty')
		 	->addValidator($loginDoesntExist)
		 	->addValidator('StringLength', false, 3, 20)
		  ->setDescription("Login between 3 and 20 alphanumerics characters.");

		$emailDoesntExist = new Zend_Validate_Db_NoRecordExists('membres', 'email');
		$email = new Zend_Form_Element_Text("email", array('size' => 25));
		$email ->setLabel('Email address')
		  ->setRequired(true)
		  ->addFilter('StripTags')
		  ->addFilter('StringTrim')
		  ->addValidator('NotEmpty')
		 	->addValidator($emailDoesntExist)
		  ->addValidator('EmailAddress')
		  ->setDescription("Require a valid email address.");

		$password = new Zend_Form_Element_Password("password", array('size' => 25));
		$password ->setLabel('Password')
		  ->setRequired(true)
		  ->addFilter('StringTrim')
		  ->addValidator('NotEmpty');

		$repassword = new Zend_Form_Element_Password("repassword", array('size' => 25));
		$repassword ->setLabel('Retype password')
		  ->setRequired(true)
		  ->addFilter('StringTrim')
		  ->addValidator('NotEmpty');
		$repassword->setDescription("Retype the previous password to prevent errors.");

		$pays = new  Zend_Form_Element_Select("pays");
		$locale = new Zend_Locale();
		$pays ->setLabel('Country')
		  	->setRequired(true)
			->addMultiOptions(Zend_Locale::getTranslationList('territory', $locale , 2))
		 	->addValidator('NotEmpty');

		
		$this->addElements(array($login,$email, $password, $repassword, $pays));
		
        
		$this->setDecorators( array( array('ViewScript', array('viewScript' => 'membre/registerform.phtml'))));
	}

	public function isValid($data)
  	{
		$password 	= $this->getElement('password');
		$password->addValidator(new Zend_Validate_PasswordMatch($data['repassword']));

		return parent::isValid($data);;
  	}
}